package cn.gson.crm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gson.crm.model.dao.ConstactOrderDAO;
import cn.gson.crm.model.dao.CustomerinfoIDAO;
import cn.gson.crm.model.dao.OrderDetailsDAO;
import cn.gson.crm.model.mapper.IConstactOrderDAO;
import cn.gson.crm.model.pojo.ConstactOrder;
import cn.gson.crm.model.pojo.Customerinfo;
import cn.gson.crm.model.pojo.Orderdetails1;
import cn.gson.crm.model.vo.ConstactOrderVO;
import cn.gson.crm.model.vo.CustomerVO;

@Service
@Transactional
public class ConstractOrderService {
	@Autowired
	private ConstactOrderDAO cDao;
	@Autowired
	private IConstactOrderDAO iDao;
	@Autowired
	private CustomerinfoIDAO cii;
	@Autowired
	private OrderDetailsDAO vDao;

	/**
	 * 添加合同订单
	 * 
	 * @param con
	 */
	public void addOrder(ConstactOrder con) {
		try {
			Customerinfo cust = cii.findOne(con.getCoCusId().longValue());
			//con.setCoCusId(BigDecimal.valueOf(cust.getCustId()));
			con.setCoCusName(cust.getCustName());
			con.setCustomerinfo(cust);
			cDao.save(con);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 编辑合同订单
	 * 
	 * @param con
	 */
	public void editOrder(ConstactOrder con) {
		try {
			Customerinfo cust = cii.findOne(con.getCoCusId().longValue());
			con.setCustomerinfo(cust);
			cDao.save(con);
			System.out.println(con.getCoCusId()+"ss");
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	public void addCood(ConstactOrder cOrder, List<Orderdetails1> list){
	//cOrder.setCustomerinfo(cii.findOne(cOrder.getCoCusId().longValue()));
		cDao.save(cOrder);
		Long id = cDao.getMaxId();
		System.out.println(id);
		ConstactOrder cOrder2 = cDao.findOne(id);
		Customerinfo customerinfo = cii.findOne(cOrder2.getCoCusId().longValue());
		cOrder2.setCustomerinfo(customerinfo);
		cDao.save(cOrder2);
		if(list != null){
			for(Orderdetails1 orderdetails1: list){
				vDao.save(orderdetails1);
				Orderdetails1 o = vDao.findOne(vDao.getMaxId());
				o.setCustomerinfo(customerinfo);
				o.setConstactOrder(cOrder2);
				vDao.save(o);
			}	
		}
	}

	/**
	 * 查询所有合同订单
	 * 
	 * @return
	 */
	public List<ConstactOrderVO> getAllOrder(int page, int rows) {
		PageRequest pRequest = new PageRequest(page - 1, rows);
		List<ConstactOrder> listpo = cDao.queryAllOrder(pRequest);
		List<ConstactOrderVO> listvo = new ArrayList<>();
		for (ConstactOrder c : listpo) {
			listvo.add(returnConstactOrderVO(c));
		}
		return listvo;
	}
	
	public ConstactOrderVO returnConstactOrderVO(ConstactOrder c) {
		ConstactOrderVO cVo = new ConstactOrderVO(c.getCoId(), c.getCoAllMoney(), c.getCoChanceId(),
				c.getCoChanceName(), c.getCoCusId(), c.getCoCusName(), c.getCoEndTime(), c.getCoGetMan(),
				c.getCoJieWay(), c.getCoManAddress(), c.getCoManPhone(), c.getCoOwner(), c.getCoPayWay(),
				c.getCoRemarks(), c.getCoSendTime(), c.getCoStartTime(), c.getCoState(), c.getCoText(),
				c.getCoTheme(), c.getCoType(), c.getCode());
		Customerinfo customerinfo = c.getCustomerinfo();
		if(customerinfo!=null) {
			CustomerVO vo = new CustomerVO(customerinfo.getCustId(), customerinfo.getCustName());
			cVo.setCoCusIdvo(vo.getCustId());
			cVo.setCoCusNamevo(vo.getCustName());
			cVo.setCustomerVO(vo);
		}
		return cVo;
	}

	/**
	 * 删除合同订单
	 * 
	 * @return
	 */
	public void deleteOrder(Long id) {
		ConstactOrder cOrder = cDao.findOne(id);
		cDao.delete(cOrder);
	}

	/**
	 * 获取订单总条数
	 * 
	 * @return
	 */
	public long orderCount() {
		return cDao.count();
	}

	/**
	 * 根据合同主题查询
	 * 
	 * @param theme
	 * @return
	 */
	public List<ConstactOrderVO> getOrderByThemeLike(String theme) {
		List<ConstactOrder> listpo = cDao.findcoTheme("%" + theme + "%");
		List<ConstactOrderVO> listvo = new ArrayList<>();
		for (ConstactOrder c : listpo) {
			listvo.add(returnConstactOrderVO(c));
		}
		return listvo;
	}

	/**
	 * 多条件查询订单
	 * 
	 * @param con
	 * @return
	 */
	public List<ConstactOrderVO> getSomeOrder(ConstactOrderVO con) {
		List<ConstactOrder> listpo = iDao.findByTrim(con);
		List<ConstactOrderVO> listvo = new ArrayList<>();
		for (ConstactOrder c : listpo) {
			listvo.add(returnConstactOrderVO(c));
		}
		return listvo;
	}

	/**
	 * 根据合同Id查询
	 * 
	 * @param theme
	 * @return
	 */
	public ConstactOrderVO getOrderById(Long id) {
		ConstactOrder cOrder = cDao.findById(id);
		return returnConstactOrderVO(cOrder);
	}
}
